//
// Created by ZLY on 2025/5/14.
//

#include "bg_star.h"

#include "../core/scene.h"

BgStar *BgStar::addBgStarChild(Object *parent, int num, float scale_far_, float scale_mid_, float scale_near_) {
    auto bg_star = new BgStar();
    bg_star->init();
    bg_star->num_ = num;
    bg_star->scale_far_ = scale_far_;
    bg_star->scale_mid_ = scale_mid_;
    bg_star->scale_near_ = scale_near_;
    bg_star->star_far_.reserve(num);
    bg_star->star_mid_.reserve(num);
    bg_star->star_near_.reserve(num);
    auto extra = Game::GetInstance().getCurrentScene()->getWorldSize() - Game::GetInstance().getScreenSize();
    for (auto i = 0; i < num; i++) {
        bg_star->star_far_.push_back(

            Game::GetInstance().randomVec2(glm::vec2(0),
                                           glm::vec2(Game::GetInstance().getScreenSize() + extra * scale_far_)));
        bg_star->star_mid_.push_back(
            Game::GetInstance().randomVec2(glm::vec2(0),
                                           glm::vec2(Game::GetInstance().getScreenSize() + extra * scale_mid_)));
        bg_star->star_near_.push_back(
            Game::GetInstance().randomVec2(glm::vec2(0),
                                           glm::vec2(Game::GetInstance().getScreenSize() + extra * scale_near_)));
    }
    if (parent) {
        parent->addChild(bg_star);
    }
    return bg_star;
}

void BgStar::update(float dt) {
    Object::update(dt);
    timer_ += dt;
    color_far_ = {
        0.5f + 0.5f * sinf(timer_ * 0.9f), 0.5f + 0.5f * sinf(timer_ * 0.8f), 0.5f + 0.5f * sinf(timer_ * 0.7f)
    };
    color_mid_ = {
        0.5f + 0.5f * sinf(timer_ * 0.8f), 0.5f + 0.5f * sinf(timer_ * 0.7f), 0.5f + 0.5f * sinf(timer_ * 0.6f)
    };
    color_near_ = {
        0.5f + 0.5f * sinf(timer_ * 0.7f), 0.5f + 0.5f * sinf(timer_ * 0.6f), 0.5f + 0.5f * sinf(timer_ * 0.5f)
    };
}

void BgStar::render() {
    Object::render();
    game_.drawPoint(star_far_, -game_.getCurrentScene()->getCameraPos() * scale_far_, color_far_);
    game_.drawPoint(star_mid_, -game_.getCurrentScene()->getCameraPos() * scale_mid_, color_mid_);
    game_.drawPoint(star_near_, -game_.getCurrentScene()->getCameraPos() * scale_near_, color_near_);
}
